package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMessageRecord;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.vf.admin.utils.StringEx.isNull;

/**
 * 系统消息
 */
@Repository
public class AppMessageRecordDao extends BaseDao<AppMessageRecord> {

    /**
     * 查询列表
     */
    public List<AppMessageRecord> findList(Integer userid,SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_message_record where 1=1");
        sbStr.append(" and state = 1 and leixing =1 ");
        if(!isNull(userid)){
            sbStr.append("or (leixing=2 and userid=? )");
            paramList.add(userid);
        }
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppMessageRecord.class, splitPage);
    }

    public List<AppMessageRecord> findListByUserIds(SplitPageBean splitPage, String userids) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_message_record where 1=1  ");
        sbStr.append(" and state = 1 and leixing =1 ");
        if(!isNull(userids)){
            sbStr.append("or (leixing=2 and userid in ( ");
            sbStr.append(userids);
            sbStr.append(" ) ) ");
        }
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppMessageRecord.class, splitPage);
    }


    /**
     * 详情
     */
    public AppMessageRecord get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_message_record where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppMessageRecord.class);
    }

    /**
     * 获取最新一条
     */
    public AppMessageRecord getNew(Integer userid) {

        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_message_record where state=1 and leixing=1 ");
        if(!isNull(userid)){
            sbStr.append("or (leixing=2 and userid=? )");
            paramList.add(userid);
        }
        sbStr.append(" order by createtime desc limit 1  ");
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppMessageRecord.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppMessageRecord haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id <= 0) {
            return;
        }
        String sql = "delete  from app_message_record where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }


    /**
     * 查询列表
     */
    public List<AppMessageRecord> list() {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_message_record where 1=1 and state = 1 and leixing =1 ");
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppMessageRecord.class);
    }



}
